import { createSlice } from "@reduxjs/toolkit";

const tabSlice = createSlice({
  name: "tab",
  initialState: {
    isCollapse: false,
    tabList: [
      {
        name: "home",
        path: "/",
        label: "首页",
      },
    ],
    currentTab: {
      name: "home",
      path: "/",
      label: "首页",
    },
  },
  reducers: {
    collapseMenu: (state) => {
      state.isCollapse = !state.isCollapse;
    },
    selectTabList: (state, { payload: val }) => {
      if (val.name !== "home") {
        if (state.tabList.findIndex((item) => item.path === val.path) === -1)
          state.tabList.push(val);
      }
    },
    removeTab: (state, { payload: val }) => {
      const index = state.tabList.findIndex((item) => item.path === val.path);
      state.tabList.splice(index, 1);
    },
    setCurrentMenu: (state, { payload: val }) => {
      state.currentTab = val;
    },
  },
});

export const { collapseMenu } = tabSlice.actions;
export default tabSlice.reducer;
